import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
// 引入多个动态路由模块
import approvals from './modules/approvals'
import departments from './modules/departments'
import employees from './modules/employees'
import permission from './modules/permissions'
import attendances from './modules/attendances'
import salarys from './modules/salarys'
import setting from './modules/settings'
import social from './modules/social_securitys'

// 动态路由表，项目中不同的用户可以访问不同的功能
export const asyncRoutes = [
  departments, // 组织架构
  setting, // 公司设置
  employees, // 员工管理
  permission, // 权限管理
  social, // 社保管理
  attendances, // 考勤管理
  salarys, // 工资管理
  approvals // 审批管理
]

// 静态路由表，项目中每个用户都可以访问的功能
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/import',
    component: Layout,
    hidden: true,
    children: [{
      path: '',
      name: 'import',
      component: () => import('@/views/employees/import.vue'),
      meta: { title: '导入excel', icon: 'dashboard' }
    }]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  }

  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  mode: 'history',
  // 管理滚动行为 如果出现滚动 切换就让 让页面回到顶部
  scrollBehavior: () => ({ y: 0 }),
  // 最终生成路由表
  // routes: [...constantRoutes, ...asyncRoutes]
  // 这里不要把8个路由 写死
  routes: [...constantRoutes]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
